package com.creatingrevolutions.vbukit.lib.kaazing;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.SystemClock;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;

@SuppressLint("SetJavaScriptEnabled") 
public class KaazingManager
{	
	Context mContext;
	Activity mActivity;
	String mServer;
	String mTopic;
	WebView webView;
	KaazingHandler mHandler;
	
	public KaazingManager(Context context, Activity activity)
	{
		this.mContext = context;
		this.mActivity = activity;
	}
	
	public void connectAndRegister(String server, String topic, 
			KaazingHandler handler)
	{
		this.mHandler = handler;
		this.mServer = server;
		this.mTopic = topic;
	
		initWebView();
	}
	
	private void initWebView()
	{
		((Activity)mContext).runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				webView = new WebView(mContext);
				webView.getSettings().setJavaScriptEnabled(true);
				webView.getSettings().setAllowContentAccess(true);
				webView.getSettings().setAppCacheEnabled(false);
				webView.getSettings().setDomStorageEnabled(true);
				webView.setInitialScale(1);
				webView.getSettings().setPluginState(PluginState.ON);
				webView.setWebChromeClient(new WebChromeClient(){});
				
				// WebSettings for WebView
				WebSettings webSettings = webView.getSettings();
				
				webSettings.setJavaScriptEnabled(true);
				webSettings.setBuiltInZoomControls(true);
				webSettings.setAllowContentAccess(true);
				webSettings.setLoadsImagesAutomatically(true);
				webSettings.setLoadWithOverviewMode(true);
				webSettings.setSupportZoom(true);
				webSettings.setUseWideViewPort(true);
				webSettings.setDomStorageEnabled(true);
				webSettings.setAppCacheEnabled(true);
				webSettings.setAppCachePath(mContext.getFilesDir()
						.getAbsolutePath() + "/cache");
				webSettings.setDatabaseEnabled(true);
				webSettings.setDatabasePath(mContext.getFilesDir()
						.getAbsolutePath() + "/databases");
				
				webView.setWebViewClient(new MyWebViewClient());
				
				defineCallbacks();

				//webView.loadUrl("file:///android_asset/kazzing.html");
				webView.loadUrl("http://54.235.209.16/virtual-bucket/testJplayer.html");
				
			}
		});
	}
	
	public WebView getView() {
		return webView;
	}
	
	private void defineCallbacks()
	{
		webView.addJavascriptInterface(this, "ACall");
	}
	
	public void sessionCreated(String message) 
	{
	    if ( mHandler != null ) {
	    	mHandler.registrationDone(true, message);
	    }
	}

	public void error(String message) 
	{
	    if ( mHandler != null ) {
	    	mHandler.errorOccured(message);
	    }
	}
	
	public void update(String message) 
	{
	    if ( mHandler != null ) {
	    	mHandler.newMessageArrived(message);
	    }
	}
	
	public void bodyLoaded() 
	{
		mActivity.runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				try {
					webView.loadUrl("javascript:WEBSOCKET_URL = '" + mServer + "';");
					webView.loadUrl("javascript:TOPIC_NAME = '" + mTopic + "';");
					webView.loadUrl("javascript:doConnect();");
				}
				catch ( Exception e ) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Handle navigation for this WebView
	 */
	private class MyWebViewClient extends WebViewClient
	{
		@Override
		public void onPageFinished(WebView view, String url)
		{
			super.onPageFinished(view, url);
			Log.i("KaazingManager", "Kaazing page load complete.");
		}
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url)
		{
			view.loadUrl(url);
			return true;
		}
	}
	
	
}
